﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Common;
using Mimoza.DBLayer.Transport;
using Mimoza.Transport;
using Mimoza.DBLayer.Transport.Client;

namespace Mimoza.DBLayer.Transport.Server
{
    public class DBContainersServer :
        ServerRepository<ContainerRecord, DBContainersCustomRequest, IDBContainerRepository>,
        IDBContainerRepository
    {
        public DBContainersServer(IDBContainerRepository dbContainers,
            CollectionManager collectionManager, ConnectionList connList)
            : base(dbContainers, collectionManager, connList)
        {
            DBContainersTranferRequest.RegisterInTransport();            
            DBContainersTranferRequest.Repository = this;
        }

        public ContainerRecord GetContainer(Guid ID)
        {
            return BaseRepository.GetContainer(ID);
        }

        public IEnumerable<ContainerRecord> GetChildsContainer(Guid containerID)
        {
            return BaseRepository.GetChildsContainer(containerID);
        }

        public IEnumerable<HostRecord> GetHosts(Guid containerID)
        {
            return BaseRepository.GetHosts(containerID);
        }

        public IEnumerable<PolicyRecord> GetPolicies(Guid containerID)
        {
            return BaseRepository.GetPolicies(containerID); ;
        }

        override public void Insert(ContainerRecord container)
        {
            BaseRepository.Insert(container);
        }

        override public void Update(ContainerRecord container)
        {
            BaseRepository.Update(container);
        }

        override public void Delete(ContainerRecord container)
        {
            BaseRepository.Delete(container);
        }

        public override TransferObject ProcessRequest(Connection conn, DBContainersCustomRequest request)
        {
            TransferObject reply = null;
            switch (request.OperationType)
            {
                case DBContainersCustomRequest.CustomOperationType.GetContainer:
                    reply = GetContainer(request);
                    break;
                case DBContainersCustomRequest.CustomOperationType.GetChildsContainer:
                    reply = GetChildsContainer(conn, request);
                    break;
                case DBContainersCustomRequest.CustomOperationType.GetHosts:
                    reply = GetHosts(conn, request);
                    break;
                case DBContainersCustomRequest.CustomOperationType.GetPolicies:
                    reply = GetPolicies(conn, request);
                    break;
            }

            return reply; 
        }

        protected TransferObject GetContainer(DBContainersCustomRequest request)
        {
            ContainerRecord record = GetContainer(new Guid(request.Argument1));

            return new RepositoryReply<ContainerRecord>(record);
        }

        protected TransferObject GetChildsContainer(Connection conn, DBContainersCustomRequest request)
        {
            IEnumerable<ContainerRecord> enumerator = GetChildsContainer(new Guid(request.Argument1));

            return RegisterCollection(enumerator,conn);
        }

        protected TransferObject GetHosts(Connection conn, DBContainersCustomRequest request)
        {
            IEnumerable<HostRecord> enumerator = GetHosts(new Guid(request.Argument1));
            
            return RegisterCollection(enumerator, conn);
        }

        protected TransferObject GetPolicies(Connection conn, DBContainersCustomRequest request)
        {
            IEnumerable<PolicyRecord> enumerator = GetPolicies(new Guid(request.Argument1));

            return RegisterCollection(enumerator, conn);
        }
    }
}
